package service

import (
	"context"
	pb "shop-demo/api/shop/bff/v1"
	"shop-demo/app/shop/bff/internal/biz"
)

func (s *ShopService) CreateOrder(ctx context.Context, req *pb.CreateOrderReq) (*pb.CreateOrderReply, error) {
	details := make([]*biz.Product, 0, len(req.Products))

	for _, product := range req.Products {
		details = append(details, &biz.Product{
			Id:  product.Id,
			Num: product.Num,
		})
	}

	order, err := s.ouc.CreateOrder(ctx, &biz.Order{
		Id:      req.UserId,
		Details: details,
	})
	if err != nil {
		return nil, err
	}
	return &pb.CreateOrderReply{
		Id:     order.Id,
		UserId: order.UserId,
	}, nil
}
func (s *ShopService) ListOrder(ctx context.Context, req *pb.ListOrderReq) (*pb.ListOrderReply, error) {
	orders, err := s.ouc.ListOrder(ctx, int(req.PageNum), int(req.PageSize))
	if err != nil {
		return nil, err
	}
	replyOrders := make([]*pb.ListOrderReply_Order, 0, len(orders))
	for _, order := range orders {
		replyOrders = append(replyOrders, &pb.ListOrderReply_Order{
			Id:     order.Id,
			UserId: order.UserId,
		})
	}
	return &pb.ListOrderReply{Orders: replyOrders}, nil
}
